
<template>
  <div class="tenantMenu_main main_container user_container add_container">
    <div v-loading="loading" class="main_main">
      <div class="SearchBar_main main_main_search">
        <div class="main_main_search_item">
          <el-input v-model="pageData.searchValue" class="search_input" placeholder="请输入物料编码或名称" clearable @keyup.enter.native="()=>{pageData.pageNum=1; Search()}" @clear="SearchList" />
          <el-button class="button_icon" icon="el-icon-search" @click.stop="()=>{pageData.pageNum=1; Search()}" />
        </div>
      </div>
      <div ref="table_container" style="margin-top:20px" class="table_container">
        <el-table
          ref="table"
          :data="tableData"
          stripe
          border
          :height="tableHeight"

          :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"

          @sort-change="
            (column) => {
              sortChange(column, this);
            }
          "
          @selection-change="
            (val) => {
              selectionChange(val, this);
            }
          "
        >
          <el-table-column type="selection" width="70" align="center" />
          <Column
            v-for="(column, index) in defaultTableColumns"
            :key="index"
            :prop="column.prop"
            :is-show="column.isShow"
            :label="column.label"
            :sortable="column.sortable"
            :width="column.width"
          />
        </el-table>
      </div>
    </div>
    <div style="background:white;border-top:1px solid rgba(51, 51, 51, 0.5)" class="main_bottom">
      <el-button type="cancel" @click.stop="cancel">取消</el-button>
      <el-button @click.stop="onSubmit">保存</el-button>
    </div>
  </div>
</template>

<script>
import { outsourceOrderDetail } from '@/api/outsourceManage/outsourceReturn'

export default {
  props: {
    layerid: {},
    id: {},
    selectedRow: {}
  },
  data() {
    return {
      materialTypeId: '',
      typeList: [],
      activeNum: 1,
      deptData: [],
      currentDeptId: '',
      currentDeptName: '',
      loading: false,
      pageId: '用户管理', // 页面唯一标识
      typeId: '',
      pageData: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: 'updateTime',
        isAsc: 'desc',
        searchValue: ''
      },
      tableDataOne: [],
      orderId: '',
      total: 1, // 数据一共有多少条
      mainId: 'uniqueId', // 表格行ID
      tableHeight: '0', // 表格高度
      tableData: [], // 表格数据
      tableColumns: [], // 表格实际用的字段
      searchColumns: [], // 搜索区实际用的字段
      selections: [], // 表格当前页所选中的行
      selectionsAll: [], // 表格所有页选中的行
      peopleList: [],
      defaultTableColumns: [
        // 表格常规字段（初始值）
        { prop: 'materialCode', label: '物料编码' },
        { prop: 'materialName', label: '物料名称' },
        { prop: 'materialModel', label: '规格型号' },
        { prop: 'materialUnitName', label: '物料单位' },
        { prop: 'outsourceNumber', label: '外协数量' },
        { prop: 'qualityDisqualificationDisposeNumber', label: '不良数量' },
        { prop: 'returnsNumber', label: '退货数量' },
        { prop: 'remark', label: '备注' }
      ]
    }
  },
  mounted() {
    this.orderId = this.id
    let isListDataOne = true
    this.selectedRow.forEach(item => {
      if (item.isListData) {
        isListDataOne = false
      }
    })
    if (!isListDataOne) {
      this.tableData = this.selectedRow
      this.selectionsAll = this.selectedRow
      this.$nextTick(() => {
        this.setSelectRow(this)// 设置行选中
      })
    } else {
      this.selectionsAll = this.selectedRow
      this.selectionsAll.forEach(item => {
        item.id = item.outsourceOrderDetailId
      })
      this.getList()
    }
    // this.selectionsAll = this.selectedRow
    this.$nextTick(() => {
      this.tableHeight = this.$refs.table_container.offsetHeight
    })
  },
  methods: {
    getList() {
      outsourceOrderDetail(this.orderId ? this.orderId : '').then(res => {
        this.tableData = res.data.detailList
        this.tableDataOne = this.$deepClone(res.data.detailList)
        this.tableData.filter(item => { // 这里的判断是因为退货数量这个字段 list后端会给值，但是在添加之前如果没有这一组数据，退货数量需要重置为0
          if (this.selectedRow.length <= 0) {
            item.returnNumber = ''
          } else {
            this.selectedRow.filter(item2 => {
              if (item.materialId !== item2.materialId) {
                item.returnNumber = ''
              }
            })
          }
        })
        this.tableData.filter(item => {
          this.selectionsAll.filter(ele => {
            if (item.materialId == ele.materialId) {
              item.returnNumber = ele.returnNumber
            }
          })
        })
        this.$nextTick(() => {
          this.setSelectRow(this)// 设置行选中
        })
      })
    },
    getMaterialInfo(item) {
      this.materialTypeId = item.id
      this.getOrderGetInfo()
    },
    cancel() {
      this.$layer.close(this.layerid)
    },
    onSubmit() {
      this.$parent.showMaterial(this.selectionsAll)
      this.$layer.close(this.layerid)
    },
    Search() {
      const arr = JSON.parse(JSON.stringify(this.selectionsAll))
      this.tableData = this.tableDataOne.filter(item => item.materialCode.indexOf(this.pageData.searchValue) !== -1 || item.materialName.indexOf(this.pageData.searchValue) !== -1)
      this.selectionsAll = arr
      this.$nextTick(() => {
        this.setSelectRow(this)// 设置行选中
      })
    },
    SearchList() {
      if (!this.pageData.searchValue) {
        this.getList()
      }
    }
  }
}
</script>

<style lang="less" scoped>
.button{
  width: 100px;
  height: 30px;
  color: #fff;
  background-color: #387bfc;
  border-radius: 4px;
  text-align: center;
  line-height: 30px;
}
.formItem_header_operate_items{
  height: 30px;
  display: flex;justify-content: center;align-items: center;
  padding: 0px 10px;font-size: 16px;border-radius: 4px;
  color: rgba(56, 123, 252, 1);background-color: rgba(235, 242, 255, 1);
  .item_name{
    font-size: 14px;padding-left: 10px;
  }
}
.formItem_header_operate_items:hover{
  background-color: rgba(235, 242, 255, 0.5);
}

.table_container{
  margin-top: 20px !important;
}
.addtype-main_bottoms{
  padding-right: 20px !important;
  margin: 10px 0;
}
.main_search_item{
  padding-right: 20px !important;
}
.main_main_search{
  padding-right: 20px !important;
}
.main_main_search_item{
  padding-left: 20px !important;
}
.main_main{
  margin: 0 !important;
  padding-top:0 !important;
  height: calc(100% - 50px) !important;

}
.button_icon{
  background:#387bfc;
  width:30px;
}
.search_input{
  width:200px;
  margin-right:10px
}
/deep/ .el-table--enable-row-hover .el-table__body tr:hover > td {
  background-color: rgba(56, 123, 252, 0.5) !important;
  color: #ffffff;
}
</style>
